package com.pp.proposalPrint.client;

import com.google.gwt.cell.client.TextInputCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

public class TextInputCellStyled extends TextInputCell {

	interface Template extends SafeHtmlTemplates {
		// orig:
		// @Template("<input type=\"text\" value=\"{0}\" tabindex=\"-1\"></input>")
		@Template("<input type=\"text\" value=\"{0}\" tabindex=\"-1\" style=\"width:{1}px\"></input>")
		//@Template("<input type=\"text\" value=\"{0}\" tabindex=\"-1\" size=\"{1}\" maxlength=\"{1}\" style=\"{2}\"></input>")
		SafeHtml input(String value, String size);
	}

	private int m_cellSize;
	private static Template template;

	public TextInputCellStyled(int cellSize) {
		//super();
		this.m_cellSize = cellSize;
	}

	@Override
	public void render(Context context, String value, SafeHtmlBuilder sb) {
		// Get the view data.
		Object key = context.getKey();
		ViewData viewData = getViewData(key);
		if (viewData != null && viewData.getCurrentValue().equals(value)) {
			clearViewData(key);
			viewData = null;
		}

		String s = (viewData != null) ? viewData.getCurrentValue() : value;
		String cellSize = "" + this.m_cellSize;
		if (s != null) {
			sb.append(getTemplate().input((s), cellSize));
		} else {
			sb.appendHtmlConstant("<input type=\"text\" tabindex=\"-1\" size=\"" + cellSize + "\"></input>");
		}
	}
	
	private Template getTemplate() {
		if (template == null) {
			template = GWT.create(Template.class);
		}
		return template;
	}

}
